home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / xpcom / nsInt64.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  11KB  |  418 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. #ifndef nsInt64_h__
  39. #define nsInt64_h__
  40.  
  41. #include "prlong.h"
  42. #include "nscore.h"
  43.  
  44. /**
  45.  * This class encapsulates full 64-bit integer functionality and
  46.  * provides simple arithmetic and conversion operations.
  47.  */
  48.  
  49. // If you ever decide that you need to add a non-inline method to this
  50. // class, be sure to change the class declaration to "class NS_BASE
  51. // nsInt64".
  52.  
  53. template<class T>
  54. class nsTInt64
  55. {
  56. public: //XXX should be private
  57.     T mValue;
  58.  
  59. public:
  60.     /**
  61.      * Construct a new 64-bit integer.
  62.      */
  63.     nsTInt64(void) : mValue(LL_ZERO) {
  64.     }
  65.  
  66.     /**
  67.      * Construct a new 64-bit integer from a 32-bit signed integer
  68.      */
  69.     nsTInt64(const PRInt32 aInt32) {
  70.         LL_I2L(mValue, aInt32);
  71.     }
  72.  
  73.     /**
  74.      * Construct a new 64-bit integer from a 32-bit unsigned integer
  75.      */
  76.     nsTInt64(const PRUint32 aUint32) {
  77.         LL_UI2L(mValue, aUint32);
  78.     }
  79.  
  80.     /**
  81.      * Construct a new 64-bit integer from a floating point value.
  82.      */
  83.     nsTInt64(const PRFloat64 aFloat64) {
  84.         LL_D2L(mValue, aFloat64);
  85.     }
  86.  
  87.     /**
  88.      * Construct a new 64-bit integer from a native 64-bit integer
  89.      */
  90.     nsTInt64(const T aInt64) : mValue(aInt64) {
  91.     }
  92.  
  93.     /**
  94.      * Construct a new 64-bit integer from another 64-bit integer
  95.      */
  96.     nsTInt64(const nsTInt64& aObject) : mValue(aObject.mValue) {
  97.     }
  98.  
  99.     // ~nsTInt64(void) -- XXX destructor unnecessary
  100.  
  101.     /**
  102.      * Assign a 64-bit integer to another 64-bit integer
  103.      */
  104.     const nsTInt64& operator =(const nsTInt64& aObject) {
  105.         mValue = aObject.mValue;
  106.         return *this;
  107.     }
  108.  
  109.     /**
  110.      * Convert a 64-bit integer to a signed 32-bit value
  111.      */
  112.     operator PRInt32(void) const {
  113.         PRInt32 result;
  114.         LL_L2I(result, mValue);
  115.         return result;
  116.     }
  117.  
  118.     /**
  119.      * Convert a 64-bit integer to an unsigned 32-bit value
  120.      */
  121.     operator PRUint32(void) const {
  122.         PRUint32 result;
  123.         LL_L2UI(result, mValue);
  124.         return result;
  125.     }
  126.  
  127.     /**
  128.      * Convert a 64-bit integer to a floating point value
  129.      */
  130.     operator PRFloat64(void) const {
  131.         PRFloat64 result;
  132.         LL_L2D(result, mValue);
  133.         return result;
  134.     }
  135.  
  136.     /**
  137.      * Convert a 64-bit integer to a native 64-bit integer.
  138.      */
  139.     operator T() const {
  140.         return mValue;
  141.     }
  142.  
  143.     /**
  144.      * Perform unary negation on a 64-bit integer.
  145.      */
  146.     const nsTInt64 operator -(void) {
  147.         nsTInt64 result;
  148.         LL_NEG(result.mValue, mValue);
  149.         return result;
  150.     }
  151.  
  152.     // Arithmetic operators
  153.  
  154.     /**
  155.      * Increment a 64-bit integer by a 64-bit integer amount.
  156.      */
  157.     nsTInt64& operator +=(const nsTInt64& aObject) {
  158.         LL_ADD(mValue, mValue, aObject.mValue);
  159.         return *this;
  160.     }
  161.  
  162.     /**
  163.      * Decrement a 64-bit integer by a 64-bit integer amount.
  164.      */
  165.     nsTInt64& operator -=(const nsTInt64& aObject) {
  166.         LL_SUB(mValue, mValue, aObject.mValue);
  167.         return *this;
  168.     }
  169.  
  170.     /**
  171.      * Multiply a 64-bit integer by a 64-bit integer amount.
  172.      */
  173.     nsTInt64& operator *=(const nsTInt64& aObject) {
  174.         LL_MUL(mValue, mValue, aObject.mValue);
  175.         return *this;
  176.     }
  177.  
  178.     /**
  179.      * Divide a 64-bit integer by a 64-bit integer amount.
  180.      */
  181.     nsTInt64& operator /=(const nsTInt64& aObject) {
  182.         LL_DIV(mValue, mValue, aObject.mValue);
  183.         return *this;
  184.     }
  185.  
  186.     /**
  187.      * Compute the modulus of a 64-bit integer to a 64-bit value.
  188.      */
  189.     nsTInt64& operator %=(const nsTInt64& aObject) {
  190.         LL_MOD(mValue, mValue, aObject.mValue);
  191.         return *this;
  192.     }
  193.  
  194.     /**
  195.      * Shift a 64-bit integer left.
  196.      */
  197.     nsTInt64& operator <<=(int aCount) {
  198.         LL_SHL(mValue, mValue, aCount);
  199.         return *this;
  200.     }
  201.  
  202.     /**
  203.      * Shift a 64-bit signed integer right.
  204.      */
  205.     nsTInt64& operator >>=(int aCount) {
  206.         LL_SHR(mValue, mValue, aCount);
  207.         return *this;
  208.     }
  209.  
  210.     // Comparison operators
  211.     /**
  212.      * Add two 64-bit integers.
  213.      */
  214.     inline const nsTInt64
  215.     operator +(const nsTInt64& aObject2) const {
  216.         return nsTInt64(*this) += aObject2;
  217.     }
  218.  
  219.     /**
  220.      * Subtract one 64-bit integer from another.
  221.      */
  222.     inline const nsTInt64
  223.     operator -(const nsTInt64& aObject2) const {
  224.         return nsTInt64(*this) -= aObject2;
  225.     }
  226.  
  227.     /**
  228.      * Multiply two 64-bit integers
  229.      */
  230.     inline const nsTInt64
  231.     operator *(const nsTInt64& aObject2) const {
  232.         return nsTInt64(*this) *= aObject2;
  233.     }
  234.  
  235.     /**
  236.      * Divide one 64-bit integer by another
  237.      */
  238.     inline const nsTInt64
  239.     operator /(const nsTInt64& aObject2) const {
  240.         return nsTInt64(*this) /= aObject2;
  241.     }
  242.  
  243.     /**
  244.      * Compute the modulus of two 64-bit integers
  245.      */
  246.     inline const nsTInt64
  247.     operator %(const nsTInt64& aObject2) const {
  248.         return nsTInt64(*this) %= aObject2;
  249.     }
  250.  
  251.     /**
  252.      * Shift left a 64-bit integer
  253.      */
  254.     inline const nsTInt64
  255.     operator <<(int aCount) const {
  256.         return nsTInt64(*this) <<= aCount;
  257.     }
  258.  
  259.     /**
  260.      * Shift right a signed 64-bit integer
  261.      */
  262.     inline const nsTInt64
  263.     operator >>(int aCount) const {
  264.         return nsTInt64(*this) >>= aCount;
  265.     }
  266.  
  267.     /**
  268.      * Determine if two 64-bit integers are equal
  269.      */
  270.     inline PRBool
  271.     operator ==(const nsTInt64& aObject2) const {
  272.         return LL_EQ(mValue, aObject2.mValue);
  273.     }
  274.  
  275.     inline PRBool
  276.     operator ==(T aValue) const {
  277.         return LL_EQ(mValue, aValue);
  278.     }
  279.  
  280.     /**
  281.      * Determine if two 64-bit integers are not equal
  282.      */
  283.     inline PRBool
  284.     operator !=(const nsTInt64& aObject2) const {
  285.         return LL_NE(mValue, aObject2.mValue);
  286.     }
  287.  
  288.     inline PRBool
  289.     operator !=(T aValue) const {
  290.         return LL_NE(mValue, aValue);
  291.     }
  292.  
  293.     /**
  294.      * Perform a bitwise AND of two 64-bit integers
  295.      */
  296.     inline const nsTInt64
  297.     operator &(const nsTInt64& aObject2) const {
  298.         return nsTInt64(*this) &= aObject2;
  299.     }
  300.  
  301.     /**
  302.      * Perform a bitwise OR of two 64-bit integers
  303.      */
  304.     inline const nsTInt64
  305.     operator |(const nsTInt64& aObject2) const {
  306.         return nsTInt64(*this) |= aObject2;
  307.     }
  308.  
  309.     /**
  310.      * Perform a bitwise XOR of two 64-bit integers
  311.      */
  312.     inline const nsTInt64
  313.     operator ^(const nsTInt64& aObject2) const {
  314.         return nsTInt64(*this) ^= aObject2;
  315.     }
  316.  
  317.     // Bitwise operators
  318.  
  319.     /**
  320.      * Compute the bitwise NOT of a 64-bit integer
  321.      */
  322.     const nsTInt64 operator ~(void) const {
  323.         nsTInt64 result;
  324.         LL_NOT(result.mValue, mValue);
  325.         return result;
  326.     }
  327.  
  328.     /**
  329.      * Compute the bitwise AND with another 64-bit integer
  330.      */
  331.     nsTInt64& operator &=(const nsTInt64& aObject) {
  332.         LL_AND(mValue, mValue, aObject.mValue);
  333.         return *this;
  334.     }
  335.  
  336.     /**
  337.      * Compute the bitwise OR with another 64-bit integer
  338.      */
  339.     nsTInt64& operator |=(const nsTInt64& aObject) {
  340.         LL_OR(mValue, mValue, aObject.mValue);
  341.         return *this;
  342.     }
  343.  
  344.     /**
  345.      * Compute the bitwise XOR with another 64-bit integer
  346.      */
  347.     nsTInt64& operator ^=(const nsTInt64& aObject) {
  348.         LL_XOR(mValue, mValue, aObject.mValue);
  349.         return *this;
  350.     }
  351.  
  352.  
  353.     /**
  354.      * Allow doing if (!some_nsInt64)
  355.      */
  356.     PRBool operator!() const {
  357.         return LL_IS_ZERO(mValue);
  358.     }
  359.     
  360. };
  361.  
  362. typedef nsTInt64<PRInt64> nsInt64;
  363. typedef nsTInt64<PRUint64> nsUint64;
  364.  
  365. /**
  366.  * Determine if one 64-bit integer is strictly greater than another, using signed values
  367.  */
  368. inline PRBool
  369. operator >(const nsInt64& aObject1, const nsInt64& aObject2) {
  370.     return LL_CMP(aObject1.mValue, >, aObject2.mValue);
  371. }
  372.  
  373. inline PRBool
  374. operator >(const nsUint64& aObject1, const nsUint64& aObject2) {
  375.     return LL_UCMP(aObject1.mValue, >, aObject2.mValue);
  376. }
  377.  
  378. /**
  379.  * Determine if one 64-bit integer is greater than or equal to another, using signed values
  380.  */
  381. inline PRBool
  382. operator >=(const nsInt64& aObject1, const nsInt64& aObject2) {
  383.     return ! LL_CMP(aObject1.mValue, <, aObject2.mValue);
  384. }
  385.  
  386. inline PRBool
  387. operator >=(const nsUint64& aObject1, const nsUint64& aObject2) {
  388.     return ! LL_UCMP(aObject1.mValue, <, aObject2.mValue);
  389. }
  390.  
  391. /**
  392.  * Determine if one 64-bit integer is strictly less than another, using signed values
  393.  */
  394. inline PRBool 
  395. operator <(const nsInt64& aObject1, const nsInt64& aObject2) {
  396.     return LL_CMP(aObject1.mValue, <, aObject2.mValue);
  397. }
  398.  
  399. inline PRBool 
  400. operator <(const nsUint64& aObject1, const nsUint64& aObject2) {
  401.     return LL_UCMP(aObject1.mValue, <, aObject2.mValue);
  402. }
  403.  
  404. /**
  405.  * Determine if one 64-bit integers is less than or equal to another, using signed values
  406.  */
  407. inline PRBool
  408. operator <=(const nsInt64& aObject1, const nsInt64& aObject2) {
  409.     return ! LL_CMP(aObject1.mValue, >, aObject2.mValue);
  410. }
  411.  
  412. inline PRBool
  413. operator <=(const nsUint64& aObject1, const nsUint64& aObject2) {
  414.     return ! LL_UCMP(aObject1.mValue, >, aObject2.mValue);
  415. }
  416.  
  417. #endif // nsInt64_h__
  418.